package com.book.controller;

import java.util.Arrays;
import java.util.List;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.book.domain.Heart;
import com.book.domain.Write;
import com.book.service.WriteService;

@Controller
public class WriteController {
	
	@Autowired
	private WriteService writeService;
	
	protected static Logger logger = Logger.getLogger("WriteController");
	
	@RequestMapping(value="write.do")
	public String writeInput(HttpServletRequest request, Write write, Heart heart, Model model){
		System.out.println("spring "+request.getParameter("writeedit"));		
		System.out.println("spring "+request.getParameter("imagev"));
		System.out.println("spring "+request.getParameter("bookname"));
		System.out.println("spring "+request.getParameter("bookgenre"));
		
		System.out.println("spring "+request.getParameter("emotion"));
		System.out.println("spring "+request.getParameter("mcode"));
		
		if(request.getParameter("bookauthor") != null && !request.getParameter("bookauthor").equals("")){
			write.setbBookAuthor(request.getParameter("bookauthor"));
		}else{
			write.setbBookAuthor("n");
		}
		
		if (request.getParameter("imagev")!=null && !request.getParameter("imagev").equals("")) {
			write.setbBimage(request.getParameter("imagev"));	
		}else{
			write.setbBimage("n");
		}

		if (!request.getParameter("writeeditsize").equals("")) {
			write.setbFontSize(request.getParameter("writeeditsize"));
		}else{
			write.setbFontSize("n");
		}

		if (!request.getParameter("writeeditcolor").equals("")) {
			write.setbFontBg(request.getParameter("writeeditcolor"));
		}else{
			write.setbFontBg("n");
		}
		if (!request.getParameter("writeedittf").equals("")) {
			write.setbFont(request.getParameter("writeedittf"));
		}else{
			write.setbFont("n");
		}
		if (!request.getParameter("writeeditarray").equals("")) {
			write.setbPosition(request.getParameter("writeeditarray"));
		}else{
			write.setbPosition("n");
		}
		
		if (!request.getParameter("writearray").equals("")) {
			write.setbCoodinate(request.getParameter("writearray"));
		}else{
			write.setbCoodinate("n");
		}
		
		if (!request.getParameter("bookurl").equals("")) {
			write.setBookurl(request.getParameter("bookurl"));
		}else{
			write.setBookurl("n");
		}

		
		write.setbBookTitle(request.getParameter("bookname"));
		write.setbBookGenre(request.getParameter("bookgenre"));
			
		write.setbContent(request.getParameter("writeedit"));		
		write.setbEmotion(request.getParameter("emotion"));		
		write.setmCode(request.getParameter("mcode"));
		
		heart.setmCode(request.getParameter("mcode"));
		
		if(request.getParameter("decodata")!=null && request.getParameter("decodata").equals("ok")){
			/*꾸미기 완료 후 하트 10 감소*/
			heart.sethPlus(0);
			heart.sethMinus(10);
			heart.sethDetail("꾸미기");
			writeService.insertHeart(heart);
		}
		
		/*글쓰기 완료 후 하트 5적립*/
		heart.sethPlus(5);
		heart.sethMinus(0);
		heart.sethDetail("글쓰기");
		writeService.insertHeart(heart);
		
		writeService.insertWrite(write);	
		System.out.println("tag Controller "+writeService.tagMatchingSelect(write));		
		model.addAttribute("tagmatching", writeService.tagMatchingSelect(write));
		
		return "writeinput";
	}
	@RequestMapping(value="heartsearch.do")
	public String heartSearch(HttpServletRequest request, Heart heart, Model model){
		System.out.println(request.getParameter("mcode"));
		heart.setmCode(request.getParameter("mcode"));
		System.out.println(writeService.heartCount(heart));
		model.addAttribute("heartc", writeService.heartCount(heart));
		return "hearttotal";
		
	}
	
	@RequestMapping(value="friendsearch.do")
	public String heartSearch(HttpServletRequest request, Model model){		
		System.out.println(request.getParameter("friendcmcode"));
		String friendCmcode = request.getParameter("friendcmcode");
		if(request.getParameter("friendcmcode") != null){		
			List<String> list = Arrays.asList(friendCmcode.substring(0, friendCmcode.length()).split(","));
			model.addAttribute("friendnickpi", writeService.selectGetNick(list));
		}		
		return "friendnick";		
	}	
	
}